using System;
using System.Collections.Generic;
using System.Text;
using AntEater.ComponentModel.BuildElements;
using System.Xml.XPath;
using System.Xml;
using AntEater.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace AntEater.Runtime.Services.Transformation
{

    /// <summary>
    /// Provides a transfomer for the OutputBuildElement.
    /// </summary>
    [BuildElementTransformer(typeof(OutputBuildElement), XPathNodeType.Element)]
    internal class OutputTransformer:BuildElementTransformerBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:OutputTransformer"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public OutputTransformer(object context)
            : base(context) {
        }

        /// <summary>
        /// Transforms an <c>BuildElement</c> into XML.
        /// </summary>
        /// <param name="buildElement">The build element.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="errors">The errors.</param>
        public override void GetXml(IBuildElement buildElement, XmlWriter writer, List<TransformationError> errors) {
            OutputBuildElement element = buildElement as OutputBuildElement;

            if (element != null) {

                BuildElementAttribute buildElementAttribute
                    = ComponentModel.Utilities.GetTypeAttributeFromObject<BuildElementAttribute>(buildElement);

                if (buildElementAttribute != null) {
                    writer.WriteStartElement(buildElementAttribute.Name);

                    if (!string.IsNullOrEmpty(element.TaskParameter)) {
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("TaskParameter"), element.TaskParameter);
                    }
                    else {
                        errors.Add(new TransformationError("TaskParameter attribute isn't specified!", element));
                    }

                    bool itemNameSpecified = false;
                    bool propertyNameSpecified = false;

                    if (!string.IsNullOrEmpty(element.ItemName)) {
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("ItemName"), element.ItemName);
                        itemNameSpecified = true;
                    }

                    if (!string.IsNullOrEmpty(element.PropertyName)) {
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("PropertyName"), element.PropertyName);
                        propertyNameSpecified = true;
                    }

                    if (!propertyNameSpecified && !itemNameSpecified) {
                        errors.Add(new TransformationError("PropertyName and ItemName attribute are not specified! One of these is required.", element));
                    }

                    if (propertyNameSpecified && itemNameSpecified) {
                        errors.Add(new TransformationError("PropertyName and ItemName attribute are specified! Only one of these is allowed.", element));
                    }

                    if (!string.IsNullOrEmpty(element.Condition)) {
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("Condition"), element.Condition);
                    }

                    this.OnGetChildElementsXml(element, writer);

                    writer.WriteEndElement();
                }
            }
        }

        /// <summary>
        /// Gets an <c>BuildElement</c> from XML.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="buildFileDefinition">The build file definition.</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public override IBuildElement GetBuildElement(XPathNavigator node, BuildFileDefinition buildFileDefinition, List<TransformationError> errors) {
            OutputBuildElement element = buildFileDefinition.CreateNewBuildElement(typeof(OutputBuildElement)) as OutputBuildElement;

            bool taskParameterFound = false;
            bool itemNameFound = false;
            bool propertyNameFound = false;

            if (node.MoveToFirstAttribute()) {
                do {
                    if (node.Name.Equals(this.GetBuildElementPropertyName("Condition"))) {
                        element.Condition = node.Value;
                    }
                    else if (node.Name.Equals(this.GetBuildElementPropertyName("TaskParameter"))) {
                        element.TaskParameter = node.Value;
                        taskParameterFound = true;
                    }
                    else if (node.Name.Equals(this.GetBuildElementPropertyName("PropertyName"))) {
                        element.PropertyName = node.Value;
                        propertyNameFound = true;
                    }
                    else if (node.Name.Equals(this.GetBuildElementPropertyName("ItemName"))) {
                        element.ItemName = node.Value;
                        itemNameFound = true;
                    }
                } while (node.MoveToNextAttribute());
            }

            if (!taskParameterFound) {
                errors.Add(new TransformationError("TaskParameter attribute is missing!", element));
            }

            if(!propertyNameFound && !itemNameFound){
                errors.Add(new TransformationError("PropertyName and ItemName attribute are missing! One of these is required.", element));
            }

            if (propertyNameFound && itemNameFound) {
                errors.Add(new TransformationError("PropertyName and ItemName attribute are present! Only one of these is allowed.", element));
            }

            return element;
        }
    }
}
